package cn.demomaster.quicktreeview;

import java.util.ArrayList;
import java.util.List;

public class TreeNode implements TreeData.TreeItemData<TreeNode> {
    private String id;
    private String parentId;
    private String lable;
    private int level;
    private TreeNode parent;
    private List<TreeNode> child;
    private boolean isExpand;
    private Object data;
    /**
     * 是否被checked选中
     */
    private boolean isChecked;
    /**
     * 是否被isSelected选中
     */
    private boolean isSelected;
    private boolean isFocused;

    public TreeNode() {
        child = new ArrayList<>();
    }

    @Override
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public boolean isExpand() {
        return isExpand;
    }

    public void setExpand(boolean expand) {
        isExpand = expand;
        if (!isExpand) {

            for (TreeNode node : child) {
                node.setExpand(isExpand);
            }
        }
    }

    public boolean isFocused() {
        return isFocused;
    }

    public void setFocused(boolean focused) {
        isFocused = focused;
        if (!isFocused) {
            for (TreeNode node : child) {
                    node.setFocused(isFocused);
            }
        }
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    @Override
    public List<TreeNode> getChild() {
        return child;
    }

    public void setChild(List<TreeNode> child) {
        this.child = child;
    }

    void addChild(TreeNode treeChildData) {
        child.add(treeChildData);
    }

    public String getLable() {
        return lable;
    }

    public void setLable(String lable) {
        this.lable = lable;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public void bindData(Object object){
        this.data = object;
    }

    public Object getData() {
        return data;
    }

    /**
     * 是否是叶子界点
     *
     * @return
     */
    public boolean isLeaf() {
        return child.size() == 0;
    }

    /**
     * 判断父节点是否展开
     *
     * @return
     */
    public boolean isParentExpand() {
        if (parent == null)
            return false;
        return parent.isExpand();
    }

    /**
     * 是否为跟节点
     *
     * @return
     */
    public boolean isRoot() {
        return parent == null;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }
}
